package com.listview;

import static com.baidu.location.demo.MainActivity.fragmentManager;
import static com.baidu.location.demo.MainActivity.gpsFragment;
import static com.fragment.GpsFragment.coordinate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.baidu.location.demo.MainActivity;
import com.baidu.location.demo.R;
import com.baidu.mapapi.map.Marker;
import com.bumptech.glide.Glide;
import com.fragment.GpsFragment;

import java.util.LinkedList;
import java.util.List;

public class MessageAdapter extends ArrayAdapter {
    private List<MessageList> data;
    private final int resourceId;
    private FragmentActivity mFragmentActivity;
    public MessageAdapter(Context mContext,int textViewResourceId,List<MessageList> mData,FragmentActivity fragmentActivity){
        super(mContext,textViewResourceId,mData);
        data=mData;
        resourceId = textViewResourceId;
        mFragmentActivity=fragmentActivity;
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MessageView messageView = null;
        if(convertView==null){
            messageView=new MessageView();
            convertView= LayoutInflater.from(getContext()).inflate(resourceId, null);
            messageView.messageImage=convertView.findViewById(R.id.messageImage);
            messageView.messageName=convertView.findViewById(R.id.messageName);
            messageView.messageName.setText(data.get(position).getName());
            convertView.setTag(messageView);
        }else{
            messageView =(MessageView) convertView.getTag();
        }

        Glide.with(getContext()).load(data.get(position).getHeadPortrait()).into(messageView.messageImage);
        convertView.setOnClickListener(view1 -> {
            Marker m= coordinate.get(data.get(position).getId());
            if(m==null){
                Toast.makeText(getContext(),"好友位置未正确获取或者未在线",Toast.LENGTH_LONG).show();
            }else{
                GpsFragment.shouPosition=m.getPosition();
                mFragmentActivity.getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .hide(MainActivity.fragment)
                        .show(gpsFragment)
                        .commit();
                MainActivity.fragment=gpsFragment;
            }
        });
        return convertView;
    }
    class MessageView{
        ImageView messageImage;
        TextView messageName;
    }
}
